﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Xml.Linq;
using System.Diagnostics;
using EzDesk.Core.Model;
using EzDesk.DesktopClient.ServiceLocator.CoreClientService;
using EzDesk.Generic;

namespace EzDesk.DesktopClient.ServiceLocator
{
    public class StubCoreClientService : ICoreClientService
    {
        public string XmlFeed = "EzDeskFeed.xml";
        public string BlankFeed = "BlankFeedInformation.xml";

        public string GetClientBaseUrl()
        {
            return string.Empty;
        }


        public Organization GetOrganizationInfo()
        {
            return new Organization()
                       {
                           Id = Guid.NewGuid(),
                           Title = "巨硬中国",
                           IconUrl = "/Images/NotificationViewHeadImage.png"
                       };
        }

        public AppFunction[] GetAllAppFunctions(Guid userId)
        {

            var functionType = new AppFunctionType()
                                            {
                                                DisplayText = "个人",
                                                IconUrl = "Images\\TaskDefaultIcon.png",
                                                Id = Guid.NewGuid(),
                                                TypeName = "个人"
                                            };

            var functionType2 = new AppFunctionType()
            {
                DisplayText = "办公",
                IconName = "Images\\TaskDefaultIcon.png",
                Id = Guid.NewGuid(),
                TypeName = "办公"
            };

            var functionCategory1 = new AppFunctionType
                                                    {
                                                        DisplayText = "发送信件",
                                                        IconUrl = "Images\\TaskDefaultIcon.png",
                                                        Id = Guid.NewGuid(),
                                                        TypeName = "信件"
                                                    };

            var functionCategory2 = new AppFunctionType
            {
                DisplayText = "休假",
                IconUrl = "/Images/NotificationViewHeadImage.png",
                Id = Guid.NewGuid(),
                TypeName = "休假"
            };

            var functionCategory3 = new AppFunctionType()
            {
                DisplayText = "休假3",
                IconUrl = "/Images/NotificationViewHeadImage.png",
                Id = Guid.NewGuid(),
                TypeName = "休假3"
            };

            var functionCategory4 = new AppFunctionType
            {
                DisplayText = "休假4",
                IconUrl = "/Images/NotificationViewHeadImage.png",
                Id = Guid.NewGuid(),
                TypeName = "休假4"
            };


            var functionCategory5 = new AppFunctionType
            {
                DisplayText = "休假5",
                IconUrl = "/Images/NotificationViewHeadImage.png",
                Id = Guid.NewGuid(),
                TypeName = "休假5"
            };


            var categroyNewMessage = Enumerable.Range(0, 2).Select(i => new AppFunction()
            {
                AppName = "Message",
                Category = functionCategory2,
                Description = "签到",
                IconUrl = "/Images/folder _ favorites.png",
                Id = Guid.NewGuid(),
                Privilege = i * 3,
                TargetUrl = "http://www.g.cn",
                Title = "签到签到签到签到签到签到",
                Type = functionType2
            }).ToArray();

            var categroyMessage2 = Enumerable.Range(0, 3).Select(i => new AppFunction()
            {
                AppName = "Message",
                Category = functionCategory2,
                Description = "发送信件",
                IconUrl = "/Images/folder _ home.png",
                Id = Guid.NewGuid(),
                Privilege = i * 3,
                TargetUrl = "http://www.g.cn",
                Title = "发送信件",
                Type = functionType
            }).ToArray();

            var categroyMessage3 = Enumerable.Range(0, 3).Select(i => new AppFunction()
            {
                AppName = "Message",
                Category = functionCategory3,
                Description = "发送信件",
                IconUrl = "/Images/folder _ home.png",
                Id = Guid.NewGuid(),
                Privilege = i * 3,
                TargetUrl = "http://www.g.cn",
                Title = "发送信件",
                Type = functionType
            }).ToArray();

            var categroyMessage4 = Enumerable.Range(0, 3).Select(i => new AppFunction()
                                                                          {
                                                                              AppName = "Message",
                                                                              Category = functionCategory4,
                                                                              Description = "发送信件",
                                                                              IconUrl = "/Images/folder _ home.png",
                                                                              Id = Guid.NewGuid(),
                                                                              Privilege = i*3,
                                                                              TargetUrl = "http://www.g.cn",
                                                                              Title = "发送信件",
                                                                              Type = functionType
                                                                          }).ToArray();

              var categroyMessage5 = Enumerable.Range(0, 3).Select(i => new AppFunction()
            {
                AppName = "Message",
                Category = functionCategory5,
                Description = "发送信件",
                IconUrl = "/Images/folder _ home.png",
                Id = Guid.NewGuid(),
                Privilege = i * 3,
                TargetUrl = "http://www.g.cn",
                Title = "发送信件",
                Type = functionType
            }).ToArray();

              var categroyMessage6 = Enumerable.Range(0, 3).Select(i => new AppFunction()
              {
                  AppName = "Message",
                  Category = functionCategory5,
                  Description = "发送信件",
                  IconUrl = "/Images/folder _ home.png",
                  Id = Guid.NewGuid(),
                  Privilege = i * 3,
                  TargetUrl = "http://www.g.cn",
                  Title = "发送信件",
                  Type = functionType2
              }).ToArray();


            return Enumerable.Range(0, 5).Select(i => new AppFunction()
                                                          {
                                                              AppName = "Message",
                                                              Category = functionCategory1,
                                                              Description = "发送信件",
                                                              IconUrl = "/Images/folder _ dropbox.png",
                                                              Id = Guid.NewGuid(),
                                                              Privilege = i * 3,
                                                              TargetUrl = "http://www.g.cn",
                                                              Title = "发送信件",
                                                              Type = functionType
                                                          }).ToArray()
                                                          .Union(categroyMessage2)
                                                          .Union(categroyNewMessage)
                                                          .Union(categroyMessage3)
                                                          .Union(categroyMessage4)
                                                          .Union(categroyMessage5)
                                                          .Union(categroyMessage6)
                                                          .ToArray();
        }

        public AppFunctionType[] GetAllAppFunctionTypes(Guid userId)
        {
            throw new NotImplementedException();
        }

        public AppFunction[] GetAppFunctionsByTypeName(Guid userId, string appFunctionTypeName)
        {
            throw new NotImplementedException();
        }

        public string GetAppFunctionShortcutIds(Guid userId)
        {
            //TODO:Mock Data
            return string.Empty;
        }

       
        public void UpdateAppFunctionShortcutSequnce(Guid userId, string shortCutIdSequnces)
        {
            //TODO: Mock Response
            return;
        }


        public string GetXmlFeedsBetweenDate(string feedTypeStr, Guid userId, DateTime from, DateTime to, bool? isRead, bool? isAvaiable, bool? hasStar)
        {
            return GenerateFeedStringWithDifferentGuidAndDifferntUpdateDate();
        }

        public string GetXmlFeedsByCount(string feedTypeStr, Guid userId, int startIndex, int count, bool? isRead, bool? isAvaiable, bool? hasStar)
        {
            return GenerateFeedStringWithDifferentGuidAndDifferntUpdateDate();
        }

        public string GetAllXmlFeedsBetweenDate(Guid userId, DateTime from, DateTime to, bool? isRead, bool? isAvaiable, bool? isMark)
        {
            return GenerateFeedStringWithDifferentGuidAndDifferntUpdateDate();
        }

        public string GetAllXmlFeedsByCount(Guid userId, int startIndex, int count, bool? isRead, bool? isAvaiable, bool? isMark)
        {
            return GenerateFeedStringWithDifferentGuidAndDifferntUpdateDate();
        }

        public void SetUserStatus(Guid userId, Guid feedId)
        {
            LogEx.TRACE("UserStatusChanged",feedId.ToString());
            return;
        }

        public Guid GetUserStatus(Guid userId)
        {
            return Guid.Empty;
        }


        public void UpdateFeedStatus(FeedStatus[] feeds, string memberString)
        {
            Debug.Write(memberString);
            Debug.Write(feeds.Count().ToString());
            Debug.Write("success");

            return;
        }

        public UserProfile GetUserProfileById(Guid userId)
        {
            throw new NotImplementedException();
        }

        public SimpleUser GetSimpleUserById(Guid userId)
        {
            throw new NotImplementedException();
        }

        public SimpleUser[] GetSimpleUserInfos(string memberstring)
        {
            throw new NotImplementedException();
        }

        public Dept GetDeptById(Guid deptId)
        {
            return new Dept() {DeptName = "软件设计部"};
        }

        private string GenerateFeedStringWithDifferentGuidAndDifferntUpdateDate()
        {
            var sb = new StringBuilder(File.ReadAllText(XmlFeed));
            XElement xelement = XElement.Parse(sb.ToString());
            int count = 0;
            var r = new Random((int)DateTime.Now.Ticks);

            foreach (var item in xelement.Descendants("feeditem"))
            {
                DateTime now = DateTime.Now;
                item.SetAttributeValue("guid", Guid.NewGuid());
                item.SetAttributeValue("updateon", now.AddSeconds(count));
                item.SetAttributeValue("createon", now.AddSeconds(count));
                item.SetAttributeValue("lastchangeon", now.AddSeconds(count));

                item.SetAttributeValue("isavailable", true);
                item.SetAttributeValue("isread", DateTime.Now.Ticks%2==1);
                count++;
            }
            return xelement.ToString();
        }

        public void UpdateFeedStatus(IList<FeedStatus> feedStatusCollection, string memberString)
        {

            Debug.Write(memberString);
            Debug.Write(feedStatusCollection.Count().ToString());
            Debug.Write("success");

            return;
        }
    }
}
